﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyKhoHang
{   
    
    public partial class frmQuanLyKho : Form
    {
        connect conn;
        public frmQuanLyKho()
        {
            conn = new connect();
            
            InitializeComponent();

            show_hang();
        }
       
        public void show_hang()
        {
            DataTable dt = new DataTable();
            SqlDataReader rd;
            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = "select * from mathang";
            cmd.Connection = conn.getconnect();

            rd = cmd.ExecuteReader();
            dt.Load(rd);
            dataGridView1.DataSource = dt;
       
        }
        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtTimKiem_KeyUp(object sender, KeyEventArgs e)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn.getconnect();
            cmd.CommandText = "select * from mathang where TenMatHang like '%" + txtTimKiem.Text + "%'";

            DataTable dt = new DataTable();
            SqlDataReader rd;
            rd = cmd.ExecuteReader();
            dt.Load(rd);
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            txtMaHang.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtTenHang.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            txtDonGia.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            txtSoLuong.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            dtpNgayNhap.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            txtXuatXu.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            txtThongSoKyThuat.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();

            txtMaHang.Enabled = false;

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult rs;
            rs= MessageBox.Show("are you sure?","", MessageBoxButtons.YesNo);
            if (rs==DialogResult.Yes)
            {

            SqlCommand cmd = new SqlCommand();

            cmd.Connection = conn.getconnect();
            cmd.CommandText = "delete from mathang where MaHang ='" + txtMaHang.Text + "'";

            cmd.ExecuteNonQuery();
            }

            //this.Dispose();
            show_hang();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = conn.getconnect();
            cmd.CommandText = "update mathang set TenMatHang='" + txtTenHang.Text + "',DonGia=" + txtDonGia.Text + ",SoLuong=" + txtSoLuong.Text + ",NgayNhap='" + dtpNgayNhap.Text + "',XuatXu='" + txtXuatXu.Text + "',ThongSoKyThuat='" + txtThongSoKyThuat.Text + "' where MaHang='"+txtMaHang.Text+"'";
            cmd.ExecuteNonQuery();

            MessageBox.Show("cập nhật thành công");
            show_hang();
            //this.Dispose();
            //frmQuanLyKho frm = new frmQuanLyKho();
           //frm.Show();
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            frmThemHang them = new frmThemHang();
            them.Show();
        }

    }
}
